package com.mongodb.mongodbboot.unitl;



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * @Title: null.java
 * @Package com.jointsky.automonitor.onlinemonitor
 * @Description: 工具类
 * @author: chenx
 * @date: 2021/8/12 14:26
 * @Copyright:
 */
public class OnlineUtils {

    /**
     * 根据时间类型获取毫秒数
     *
     * @param dateType
     * @param dataTime
     * @return
     */
    public static long getDateMillisByDateType(String dateType, LocalDateTime dataTime) {
        long timeMills;
        switch (dateType) {
            case "0": //日
                timeMills = dataTime.withHour(0).withMinute(0).withSecond(0).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
                break;
            case "1": //小时
                timeMills = dataTime.withMinute(0).withSecond(0).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
                break;
            case "2": //分钟
            default: //实时
                timeMills = dataTime.withSecond(0).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
                break;
        }
        return timeMills;
    }


    /**
     * 开始时间
     *
     * @param dateType
     * @param dataTime
     * @return
     */
    public static long getStartTimeMillisByDateType(String dateType, LocalDateTime dataTime) {
        long timeMills;
        switch (dateType) {
            case "0": //日
                timeMills = dataTime.withHour(0).withMinute(0).withSecond(0).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
                break;
            case "1": //小时
                timeMills = dataTime.withMinute(0).withSecond(0).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
                break;
            case "2": //分钟
                timeMills = dataTime.withSecond(0).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
                break;
            default: //实时
                timeMills = dataTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
                break;
        }
        return timeMills;
    }

    /**
     * 结束时间
     *
     * @param dateType
     * @param dataTime
     * @return
     */
    public static long getEndTimeMillisByDateType(String dateType, LocalDateTime dataTime) {
        long timeMills;
        switch (dateType) {
            case "0": //日
                timeMills = dataTime.withHour(23).withMinute(59).withSecond(59).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
                break;
            case "1": //小时
                timeMills = dataTime.withMinute(59).withSecond(59).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
                break;
            case "2": //分钟
                timeMills = dataTime.withSecond(59).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
                break;
            default: //实时
                timeMills = dataTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
                break;
        }
        return timeMills;
    }


    /**
     * 处理结束时间为23:59:59
     *
     * @param dateType
     * @param dataTime
     * @return
     */
    public static LocalDateTime processEndTime(String dateType, LocalDateTime dataTime) {
        switch (dateType) {
            case "0": //日
                dataTime = dataTime.withHour(23).withMinute(59).withSecond(59);
                break;
            case "1": //小时
                dataTime = dataTime.withMinute(59).withSecond(59);
                break;
            case "2": //分钟
                dataTime = dataTime.withSecond(59);
                break;
        }
        return dataTime;
    }

    //异常线索和自动监控定义不一样
    public static String dateType(String dateType) {
        switch (dateType) {
            case "0": //日
                dateType ="1";
                break;
            case "1": //小时
                dateType ="2";
                break;
            case "2": //分钟
                dateType ="3";
                break;
            case "3": //分钟
                dateType ="4";
                break;
        }
        return dateType;
    }

    public static LocalDateTime processBeginTime(String dateType, LocalDateTime dataTime) {
        switch (dateType) {
            case "2": //分钟
                dataTime = dataTime.withSecond(0);
                break;
        }
        return dataTime;
    }

    public static Date getDateTime(String dateType,String dataTime){
        Date dateTime = null;
        String format = "";
        switch (dateType) {
            case "0": //日
                format= "yyyy-MM-dd";
                break;
            case "1": //小时
                format= "yyyy-MM-dd HH";
                break;
            case "2": //分钟
            case "4": //原始监测
                format= "yyyy-MM-dd HH:mm";
                break;
            case "3": //分钟
                format= "yyyy-MM-dd HH:mm:ss";
                break;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            dateTime = sdf.parse(dataTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateTime;
    }

    public static String DatetoStr(LocalDateTime str,String dateType){
        String format = "";
        switch (dateType) {
            case "0": //日
                format= "yyyy-MM-dd";
                break;
            case "1": //小时
                format= "yyyy-MM-dd HH";
                break;
            case "2": //分钟
                format= "yyyy-MM-dd HH:mm";
                break;
            case "3": //分钟
                format= "yyyy-MM-dd HH:mm:ss";
                break;
        }
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
        String formattedDate = str.format(dateTimeFormatter);
        return formattedDate;

    }

    public static String startTimeStr(String str,String dateType){
        switch (dateType) {
            case "0": //日
                str= str+" 00:00:00";
                break;
            case "1": //小时
                str= str+":00:00";
                break;
            case "2": //分钟
                str= str+":00";
                break;
        }
        return str;
    }

    public static String endTimeStr(String str,String dateType){
        switch (dateType) {
            case "0": //日
                str= str+" 23:59:59";
                break;
            case "1": //小时
                str= str+":59:59";
                break;
            case "2": //分钟
                str= str+":59";
                break;
        }
        return str;
    }

    public static LocalDateTime parse(String str,String dateType){
        String format = "";
        switch (dateType) {
            case "0": //日
                format= "yyyy-MM-dd";
                break;
            case "1": //小时
                format= "yyyy-MM-dd HH";
                break;
            case "2": //分钟
                format= "yyyy-MM-dd HH:mm";
                break;
            case "3": //分钟
                format= "yyyy-MM-dd HH:mm:ss";
                break;
        }
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        if(dateType.equals("0")){
            DateTimeFormatter DATEFORMATTER = new DateTimeFormatterBuilder().append(df)
                    .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
                    .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
                    .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
                    .toFormatter();
            return LocalDateTime.parse(str, DATEFORMATTER);
        }else {
            return LocalDateTime.parse(str,df);
        }

    }


    //多个时间段去重
    public static List<String[]> fib(List<String[]>listIn){
        Collections.sort(listIn, new Comparator<String[]>() {
            @Override
            public int compare(String[] o1, String[] o2) {
                return o1[0].compareTo(o2[0]);
            }
        });
        List<String[]> listOut = new ArrayList<String[]>();
        String begin = "";
        String end = "";
        for (int i = 0,size = listIn.size();i < size;i++){
            String[] tmp = listIn.get(i);
            if(i==0){
                begin = tmp[0];
                end = tmp[1];
            }else{
                if(tmp[0].compareTo(end) <=0 ){
                    if(tmp[1].compareTo(end) >0){
                        end = tmp[1];
                    }
                }else{
                    String[] str = {begin,end};
                    listOut.add(str);
                    begin = tmp[0];
                    end = tmp[1];
                }
            }
            if(i==(size-1)){
                String[] str = {begin,end};
                listOut.add(str);
            }
        }
        return listOut;
    }

    public static boolean getTimeHb(String end, String start, String pattern, String dateType){

        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            Date parse = sdf.parse(end);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(parse);
            if(dateType.equals("1")){
                calendar.add(Calendar.HOUR,1);
            }else if(dateType.equals("0")){
                calendar.add(Calendar.DATE,1);
            }else if(dateType.equals("2")){
                calendar.add(Calendar.MINUTE,1);
            }
            Date time = calendar.getTime();
            end = sdf.format(time);
            if(end.compareTo(start) ==0){
                return true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return false;

    }


    public static List<Map<String, Object>> getTimeFlag(List<Map<String, Object>> nhTime, String pattern, String dateType){
        List<Map<String, Object>> nh = new ArrayList<>();
        for(int i=1;i<nhTime.size();i ++){
            Map<String, Object> map = nhTime.get(i-1);
            Map<String, Object> mapnext = nhTime.get(i);
            String end = map.get("endTime").toString();
            String startNext = mapnext.get("startTime").toString();
            //如果第一个数组的结束时间和第二个数组的开始时间连续 说明可以合并
            boolean timeHb = getTimeHb(end, startNext, pattern, dateType);
            if(timeHb){
                Map<String, Object> mm = new HashMap<>();
                mm.put("startTime",map.get("startTime").toString());
                mm.put("endTime",mapnext.get("endTime").toString());
                nh.add(mm);
            }
        }

        List<Map<String, Object>> nhTime1 = getNhTime(nhTime, nh);
        return nhTime1;
    }

    public static List<Map<String, Object>> getNhTime(List<Map<String, Object>> jzTime, List<Map<String, Object>> mpTime){
        List<Map<String, Object>>  list = new ArrayList<>();
        List<String[]> listIn = new ArrayList<String[]>();
        for(Map<String, Object> jz:jzTime){
            String[] a = {jz.get("startTime").toString(),jz.get("endTime").toString()};
            listIn.add(a);
        }

        for(Map<String, Object> jz:mpTime){
            String[] a = {jz.get("startTime").toString(),jz.get("endTime").toString()};
            listIn.add(a);
        }

        List<String[]> fib = fib(listIn);
        for(String[] ss:fib){
            Map<String, Object> map = new HashMap<>();
            map.put("startTime",ss[0]);
            map.put("endTime",ss[1]);
            list.add(map);
        }

        return list;

    }

}
